package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Commodity;
import cn.wolfcode.trip.base.domain.News;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    public void register(User user) {
        //检验email的唯一性
        User tuser = userMapper.selectUserByEmailAndPassword(user.getEmail(), null);

        if (tuser != null) {
            throw new RuntimeException("用户已经注册,请重新输入");
        }

        //设置默认属性
        user.setCoverImgUrl("/img/user/bg.jpeg");
        user.setHeadImgUrl("/img/user/head.jpg");
        //用户注册
        userMapper.insert(user);
    }

    @Override
    public User login(String email, String password) {
        //登录操作,通过传入的数据获取用户对象,共享到session中
        User user = userMapper.selectUserByEmailAndPassword(email, password);
        //分析:此处无法导入UserContext类,不太方便将对象共享到session中
        //此时将这个对象返回到Controller中,让其处理
        return user;
    }


    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<User> list = userMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public User getUserByUserId(Long userId) {

        User user = userMapper.selectByPrimaryKey(userId);
        return user;
    }

    @Override
    public boolean changeFans(Long userId, Long authorId) {
        //获取作者
        User author = userMapper.selectByPrimaryKey(authorId);
        //获取作者粉丝数
        Long fans = author.getFans();

        //获取当前用户
        User user = userMapper.selectByPrimaryKey(userId);
        //获取用户关注数
        Long follows = user.getFollows();

        //根据用户id获取用户关注的所有作者id
        List<Integer> list = userMapper.selectAuthorByUserId(userId);
        for (Integer integer : list) {
            //判断用户是否已经关注该作者
            if (new Long(authorId).intValue() == integer.intValue()) {
                //如果已经关注,本次点击表示想取消关注
                //取消关注
                userMapper.deleteFansRelation(userId, authorId);
                //用户关注人数-1
                follows = follows - 1;
                user.setFollows(follows);
                userMapper.updateFollows(user);
                //作者粉丝人数-1
                fans = fans - 1;
                author.setFans(fans);
                userMapper.updateFans(author);
                return false;
            }
        }
        //新增粉丝
        userMapper.insertFansRelation(userId, authorId);
        fans = fans + 1;
        author.setFans(fans);
        userMapper.updateFans(author);

        //新增关注
        follows = follows + 1;
        user.setFollows(follows);
        userMapper.updateFollows(user);
        return true;

    }

    @Override
    public boolean relations(Long userId, Long authorId) {
        Integer relations = userMapper.relations(userId, authorId);
        if (relations == null) {
            return false;
        }
        return true;

    }
    @Override
    public List<Travel> getTravelCollections(Long userId) {
        return userMapper.getTravelCollections(userId);
    }
    @Override
    public List<News> getNewsCollections(Long userId) {
        return userMapper.getNewsCollections(userId);
    }

    @Override
    public boolean travelRelations(Long userId, Long travelId) {
        Integer relations=userMapper.travelRelations(userId, travelId);
        if (relations==null) {
            return false;
        }
        return true;
    }

    @Override
    public boolean newsRelations(Long userId, Long newsId) {
        Integer relations=userMapper.newsRelations(userId, newsId);
        if (relations == null) {
            return false;
        }
        return true;
    }

    @Override
    public boolean changeTravelCollections(Long userId, Long travelId) {
        //获取当前用户
        User user = userMapper.selectByPrimaryKey(userId);
           //根据用户id获取用户收藏的所有作品
        List<Integer> list = userMapper.selectTravelByUserId(userId);
        for (Integer integer : list) {
            if (integer!=null) {
                //判断用户是否已经关注该作者
                if (travelId.intValue()==integer) {
                    //如果已经关注,本次点击表示想取消关注
                    //取消关注
                    userMapper.deleteTravelRelation(userId, travelId);
                    return false;
                }
            }
        }
        //新增收藏作品
        userMapper.insertTravelRelation(userId, travelId);
        return true;
    }

    @Override
    public boolean changeNewsCollections(Long userId, Long newsId) {
        //获取当前用户
        User user = userMapper.selectByPrimaryKey(userId);
        //根据用户id获取用户收藏的所有咨询
        List<Integer> list = userMapper.selectNewsByUserId(userId);
        for (Integer integer : list) {
            if (integer!=null) {
                //判断用户是否已经收藏咨询
                if (newsId.intValue()==integer) {
                    //如果已经关注,本次点击表示想取消关注
                    //取消关注
                    userMapper.deleteNewsRelation(userId, newsId);
                    return false;
                }
            }
        }
        //新增收藏作品
        userMapper.insertNewsRelation(userId, newsId);
        return true;
    }


}
